package com.glela.talent.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import com.glela.platform.constant.ErrorEnum;
import com.glela.platform.mapper.BaseRestrictionsMapper;
import com.glela.platform.model.BaseRestrictions;
import com.glela.platform.model.response.ResponseBaseModel;
import com.glela.talent.mapper.UserDailyLogMapper;
import com.glela.talent.service.TalentService;
import com.glela.talent.service.UserDailyLogService;

/**
 * 
 * @author MaoZeYao
 *
 */
@Service("userDailyLogService")
public class UserDailyLogServiceImpl implements UserDailyLogService {

	@Autowired
	private UserDailyLogMapper userDailyLogMapper;
	
	@Autowired
	private BaseRestrictionsMapper baseRestrictionsMapper;
	
	@Autowired
	private TalentService talentService;
	
	@Override
	public ResponseEntity<?> addUserDailyLog(Long userId, int type) throws Exception {
		List<BaseRestrictions> base = baseRestrictionsMapper.selectBaseRestrictions();
		int status = userDailyLogMapper.selectDailyLogByType(userId, type);
		for(BaseRestrictions restriction : base){
			if(restriction.getId() == type){
				if(status >= restriction.getRestrictions()){
					// 当日已达上限
					if (type == 1) {
						return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C20163), HttpStatus.OK);
					}
					if (type == 2) {
						return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C20161), HttpStatus.OK);
					}
					if (type == 3) {
						return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C20162), HttpStatus.OK);
					}
				}
				// 增加日志
				int logInsert = userDailyLogMapper.insertIntoUserDailyLog(userId, type);
				if(logInsert <= 0){
					throw new Exception();
				}
				// 增加经验值 升级
				return talentService.increaseTalentExperience(userId, restriction.getExperience());
			}
		}
		return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C10000), HttpStatus.OK);
	}
}
